using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace One_Crappy_RPG
{
    public class WorldScreen : GameScreen
    {
        ContentManager content;
        TileLayer WorldMapLayer;
        TileMap WorldMap;
        Hero Player = new Hero();
        Random random = new Random();
        Texture2D BattleBG;
        Texture2D BattleBGSky;
        Camera camera = new Camera();
        int NextBattle;

        List<Hero> Party = new List<Hero>();

        public WorldScreen()
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            NextBattle = random.Next(5, 20);
        }

        public override void LoadContent()
        {
            base.LoadContent();

            if (content == null)
            {
                content = ScreenManager.content;
            }

            WorldMap = TileMap.FromFile(content, "TestCity.ocm");

            //Start the player on this map
            Player.CurrentMap = WorldMap;

            //Set up the encounter dictionary for the world map
            List<Monster> battles = new List<Monster>();
            battles.Add(new Monster("Slime", 1));
            battles.Add(new Monster("Bear", 1));
            battles.Add(new Monster("Skeleton", 1));
            WorldMap.Encounters.Add(1, battles);

            //Give the player characters textures
            Player.Texture = content.Load<Texture2D>("Art/People/Hero");

            //The battle background for this map
            BattleBG = content.Load<Texture2D>("Art/Combat/WorldMapBattleScreen");
            BattleBGSky = content.Load<Texture2D>("Art/Combat/Sky");

            //Add the players to the party now that they have textures
            Party.Add(Player);
        }

        public override void HandleInput()
        {
            InputManager.Update();

            //Don't bother taking more movement if we're already moving
            if (!Player.IsMoving)
            {
                //Player.MoveTarget is clamped to CurrentMap in Hero.cs
                if (InputManager.IsKeyPressed(Keys.Right))
                {
                    Player.MoveTarget.X = Player.MapPosition.X + TileMap.TileSize;
                }
                else if (InputManager.IsKeyPressed(Keys.Left))
                {
                    Player.MoveTarget.X = Player.MapPosition.X - TileMap.TileSize;
                }
                else if (InputManager.IsKeyPressed(Keys.Up))
                {
                    Player.MoveTarget.Y = Player.MapPosition.Y - TileMap.TileSize;
                }
                else if (InputManager.IsKeyPressed(Keys.Down))
                {
                    Player.MoveTarget.Y = Player.MapPosition.Y + TileMap.TileSize;
                }
            }
        }

        public override void UnloadContent()
        {
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
                SpriteSortMode.Deferred, SaveStateMode.None, camera.CameraTransform);


            //TODO:  Cull tiles not on screen to save GPU power and allow for
            //ridiculously large maps
            WorldMap.Draw(ScreenManager.spriteBatch);
            Player.Draw(ScreenManager.spriteBatch);

            ScreenManager.spriteBatch.End();

            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            
            //If player has taken enough steps to get into the next random battle
            if (Hero.StepCounter > NextBattle)
            {
                //Set counters to 0
                NextBattle = 0;
                Hero.StepCounter = 0;

                //Shut off the world map
                isActive = false;
                hasFocus = false;

                //Open a battle screen
                BattleScreen battle = new BattleScreen(Party, 
                    WorldMap.GetEncounter(Party[0]), 
                    this, 
                    BattleBG, 
                    BattleBGSky);

                ScreenManager.AddScreen(battle);

                //Don't continue this update loop
                return;
            }

            //0 means a battle just occured, so generate a new number of steps
            //til the next battle
            if (NextBattle == 0)
            {
                NextBattle = random.Next(5, 20);
                foreach (Hero player in Party)
                    player.InBattle = false;
            }

            Player.Update(gameTime);
            camera.Update(Player, WorldMap, this.ScreenManager);

            base.Update(gameTime);
        }
    }
}
